package net.wanho.service;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.SecureUtil;
import lombok.RequiredArgsConstructor;
import net.wanho.constant.UserConstant;
import net.wanho.dto.LoginUserDto;
import net.wanho.exception.LoginErrorException;
import net.wanho.mapper.UserInfoMapper;
import net.wanho.mapper.UserRoleMapper;
import net.wanho.po.UserInfo;
import net.wanho.vo.LoginUserVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.security.auth.login.LoginException;

@Service
@RequiredArgsConstructor
@Transactional
public class LoginServiceImpl implements LoginService {

    private final UserInfoMapper userInfoMapper;
    @Override
    public LoginUserVo login(LoginUserDto loginUserDto) {
        //判断用户
        UserInfo user= (UserInfo) userInfoMapper.selectByUsername(loginUserDto.getUserName());
        if (ObjectUtil.isEmpty( user)){
            throw new LoginErrorException("用户不存在");
        }
        if (!user.getUserPwd().equals(SecureUtil.md5(loginUserDto.getUserPwd()))){
            throw new LoginErrorException("密码错误");
        }
        if (!user.getUserState().equals(UserConstant.STATE_DISABLE)){
            throw new LoginErrorException("用户被禁用");
        }

        return null;
    }
}
